import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.lang.model.element.Element;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Mp3zing {
	public static AccentRemover artest = new AccentRemover();
	

	public static void main(String[] arguments) throws Exception {
		// BufferedReader in = new BufferedReader(new
		// InputStreamReader(url.openStream(), "UTF8"));
		int LENGTH = getSongs().size();
		
		ArrayList<SongVO> mainListSong = new ArrayList<SongVO>();
		ArrayList<SongVO> mainArrayListSong = new ArrayList<SongVO>();
		ArrayList<SongVO> arrayListSong = new ArrayList<SongVO>();
		
		mainListSong.addAll(getSongs());
		mainArrayListSong.addAll(getSongs());
		
		arrayListSong.addAll(mainArrayListSong);
		for(int i = 0; i < LENGTH; i++) {
			System.out.println(i + mainArrayListSong.get(i).getName());
			arrayListSong.get(i).setName(AccentRemover.toUrlFriendly(arrayListSong.get(i).getName().substring(0, arrayListSong.get(i).getName().length() - 1)));
			System.out.println(i + mainArrayListSong.get(i).getName());
		}
		
		arrayListSong.addAll(nhacso(mainArrayListSong));
		arrayListSong.addAll(yeucahat(arrayListSong));
		arrayListSong.addAll(mp3zing(arrayListSong));

//		for (int j = 0; j < arrayListSong.size(); j++) {
//			System.out.println("Id : " + arrayListSong.get(j).getId());
//			System.out.println("Name : " + arrayListSong.get(j).getName());
//			System.out.println("Lyric : " + arrayListSong.get(j).getLyric());
//		}
//
		

//		AccentRemover.toUrlFriendly(tempName.substring(0, tempName.length() - 1))
//				
				
				
				
		FileOutputStream fileOutStream;
		fileOutStream = new FileOutputStream("output.txt");

		BufferedWriter bufferedWriter = new BufferedWriter(
				new OutputStreamWriter(fileOutStream, "Unicode"));

//		for (int i = 0; i < LENGTH; i++) {
//			s = "";
//			// Read html page
//			BufferedReader in = new BufferedReader(
//					new InputStreamReader(new URL(prefixURL
//							+ arrayListSong.get(i).getName()).openStream(),
//							"UTF8"));
//			// Set html page source to String s
//			while ((inputLine = in.readLine()) != null) {
//				s += inputLine;
//			}
//
//			org.jsoup.nodes.Document doc = Jsoup.parse(s);
//
//			// Get all link search song and save to songURL array
//			Elements options4 = doc.select("a._trackLink");
//			songURL = new String[options4.size()];
//			for (int j = 0; j < options4.size(); j++) {
//				org.jsoup.nodes.Element options3 = doc.select("a._trackLink")
//						.get(j);
//				String attr3 = options3.attr("href");
//				songURL[j] = prefixSongURL + attr3;
//			}
//
//			// Set song lyric default
//			songLyric[i] = " ";
//
//			// Save lyric to songLyric array
//			for (int k = 0; k < songURL.length; k++) {
//				s = "";
//				BufferedReader in2 = new BufferedReader(new InputStreamReader(
//						new URL(songURL[k]).openStream(), "UTF8"));
//				while ((inputLine = in2.readLine()) != null) {
//					s += inputLine;
//				}
//				org.jsoup.nodes.Document doc2 = Jsoup.parse(s);
//				Elements options = doc2.select("p._lyricContent");
//				String attr = options.text();
//				System.out.println(attr);
//				if (!attr.equals("")
//						&& artest.toRemoveVNese(attr).startsWith(
//								artest.toRemoveVNese(getSongs().get(i)
//										.getLyric()))) {
////					songLyric[i] = attr;
//					arrayListSong.get(i).setLyric(attr);
//					break;
//				}
//			}
//		}

		int dem = 0;
		
		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 
			// root elements
			org.w3c.dom.Document doc = docBuilder.newDocument();
			org.w3c.dom.Element rootElement = doc.createElement("songlist");
			doc.appendChild(rootElement);
		
			for (int i = 0; i < LENGTH; i++) {
				if (arrayListSong.get(i).getLyric().equals("")) {
					dem++;
				}
				
				// song elements
				org.w3c.dom.Element song = doc.createElement("song");
				rootElement.appendChild(song);
								
				// id
				org.w3c.dom.Element id = doc.createElement("id");
				id.appendChild(doc.createTextNode(arrayListSong.get(i).getId()));
				song.appendChild(id);
				
				// name
				org.w3c.dom.Element name = doc.createElement("name");
				name.appendChild(doc.createTextNode(mainListSong.get(i).getName()));
				song.appendChild(name);
		 
				// author
				org.w3c.dom.Element author = doc.createElement("author");
				author.appendChild(doc.createTextNode(mainArrayListSong.get(i).getAuthor()));
				song.appendChild(author);
		 
				// lyric
				org.w3c.dom.Element lyric = doc.createElement("lyric");
				if(!arrayListSong.get(i).getLyric().equals("")){
					lyric.appendChild(doc.createTextNode(arrayListSong.get(i).getLyric()));
				} else {
					lyric.appendChild(doc.createTextNode(mainArrayListSong.get(i).getLyric()));
				}
				
				song.appendChild(lyric);
//				bufferedWriter.write(arrayListSong.get(i).getLyric());
//				bufferedWriter.newLine();
			}
			
			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File("file2.xml"));
	 
			// Output to console for testing
			// StreamResult result = new StreamResult(System.out);
	 
			transformer.transform(source, result);
	 
			System.out.println("File saved!");
				
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		  }

//		bufferedWriter.close();
		
		
		
		
		System.out.println("FINISH FINISH FINISH FINISH FINISH FINISH FINISH FINISH ");
		System.out.println("Array length is " + arrayListSong.size());
		System.out.println("Emmpty is " + dem);
	}

	public static ArrayList<SongVO> getSongs() {

		ArrayList<SongVO> tempSongArray = new ArrayList<SongVO>();
		SongVO song;
		String tempName;
		String tempAuthor;
		String tempId;
		String tempLyric;

		try {

			String fileName = "file.xml";
			File fXMLFile = new File(fileName);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			org.w3c.dom.Document doc = dBuilder.parse(fXMLFile);
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("song");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					org.w3c.dom.Element eElement = (org.w3c.dom.Element) nNode;

					tempId = eElement.getElementsByTagName("id").item(0)
							.getTextContent();
					tempName = eElement.getElementsByTagName("name").item(0)
							.getTextContent();
					tempAuthor = eElement.getElementsByTagName("author").item(0)
							.getTextContent();
					tempLyric = eElement.getElementsByTagName("lyric").item(0)
							.getTextContent();

					song = new SongVO(tempId, tempName, tempAuthor, tempLyric);

					tempSongArray.add(song);

					tempName = "";
					tempId = "";
					tempLyric = "";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tempSongArray;
	}

	
	
	public static ArrayList<SongVO> mp3zing(ArrayList<SongVO> arrayListSong) throws UnsupportedEncodingException, MalformedURLException, IOException {
		String inputLine;
		String[] songURL;
		String s = "";

		int LENGTH = arrayListSong.size();

		String prefixSongURL = "http://mp3.zing.vn";
		String prefixURL = "http://mp3.zing.vn/tim-kiem/bai-hat.html?q=";
		
		for (int i = 0; i < LENGTH; i++) {
			if(arrayListSong.get(i).getLyric().length() < 100){
				s = "";
				// Read html page
				BufferedReader in = new BufferedReader(
						new InputStreamReader(new URL(prefixURL
								+ arrayListSong.get(i).getName()).openStream(),
								"UTF8"));
				// Set html page source to String s
				while ((inputLine = in.readLine()) != null) {
					s += inputLine;
				}
	
				org.jsoup.nodes.Document doc = Jsoup.parse(s);
	
				// Get all link search song and save to songURL array
				Elements options4 = doc.select("a._trackLink");
				int z = options4.size();
				
				if(z < 5) {
					songURL = new String[z];
					for (int j = 0; j < z; j++) {
						org.jsoup.nodes.Element options3 = doc.select("a._trackLink")
								.get(j);
						String attr3 = options3.attr("href");
						songURL[j] = prefixSongURL + attr3;
					}
				} else {
					songURL = new String[5];
					for (int j = 0; j < 5; j++) {
						org.jsoup.nodes.Element options3 = doc.select("a._trackLink")
								.get(j);
						String attr3 = options3.attr("href");
						songURL[j] = prefixSongURL + attr3;
					}
				}
				
	
				// Set song lyric default
	//			songLyric[i] = " ";
	
				// Save lyric to songLyric array
				for (int k = 0; k < (z>5?5:z); k++) {
					s = "";
					BufferedReader in2 = new BufferedReader(new InputStreamReader(
							new URL(songURL[k]).openStream(), "UTF8"));
					while ((inputLine = in2.readLine()) != null) {
						s += inputLine;
					}
					org.jsoup.nodes.Document doc2 = Jsoup.parse(s);
					Elements options = doc2.select("p._lyricContent");
					String attr = options.text();
					if (!attr.equals("")
							&& AccentRemover.toRemoveVNese(attr).startsWith(
									AccentRemover.toRemoveVNese(arrayListSong.get(i)
											.getLyric()))) {
						arrayListSong.get(i).setLyric(attr.substring(10));
						break;
					} else {
						arrayListSong.get(i).setLyric("");
					}
				}
			}
		}
		return arrayListSong;
	}

	public static ArrayList<SongVO> nhacso(ArrayList<SongVO> arrayListSong) throws UnsupportedEncodingException, MalformedURLException, IOException {
		String inputLine;
		String[] songURL;
		String s = "";

		int LENGTH = arrayListSong.size();

//		String prefixSongURL = "http://nhacso.net";
		String prefixURL = "http://nhacso.net/tim-kiem/";
		String suffixURL = ".html";
		
		for (int i = 0; i < LENGTH; i++) {
			s = "";
			// Read html page
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new URL(prefixURL + arrayListSong.get(i).getName() + suffixURL).openStream(), "UTF8"));
			
			// Set html page source to String s
			while ((inputLine = in.readLine()) != null) {
				s += inputLine;
			}

			org.jsoup.nodes.Document doc = Jsoup.parse(s);

			// Get all link search song and save to songURL array
			Elements options4 = doc.select("a.name_music_2");
			int z = options4.size();
			
			if(z < 5) {
				songURL = new String[options4.size()];
				for (int j = 0; j < options4.size(); j++) {
					org.jsoup.nodes.Element options3 = doc.select("a.name_music_2").get(j);
					String attr3 = options3.attr("href");
					songURL[j] = attr3;
				}
			} else {
				songURL = new String[5];
				for (int j = 0; j < 5; j++) {
					org.jsoup.nodes.Element options3 = doc.select("a.name_music_2").get(j);
					String attr3 = options3.attr("href");
					songURL[j] = attr3;
				}
			}

			// Save lyric to songLyric array
			for (int k = 0; k < (z>5?5:z); k++) {
				s = "";
				BufferedReader in2 = new BufferedReader(new InputStreamReader(
						new URL(songURL[k]).openStream(), "UTF8"));
				while ((inputLine = in2.readLine()) != null) {
					s += inputLine;
				}
				org.jsoup.nodes.Document doc2 = Jsoup.parse(s);
				Elements options = doc2.select("p.desc");
				String attr = options.text();
				System.out.println(attr);
				if (!attr.equals("")
						&& AccentRemover.toRemoveVNese(attr).startsWith(
								AccentRemover.toRemoveVNese(arrayListSong.get(i)
										.getLyric()))) {
					arrayListSong.get(i).setLyric(attr);
					break;
				} else {
					arrayListSong.get(i).setLyric("");
				}
			}
		}
		return arrayListSong;
	}
	
	public static ArrayList<SongVO> yeucahat(ArrayList<SongVO> arrayListSong) throws UnsupportedEncodingException, MalformedURLException, IOException {
		String inputLine;
		String[] songURL;
		String s = "";

		int LENGTH = arrayListSong.size();

		String prefixSongURL = "http://music.yeucahat.com/";
		String prefixURL = "http://search.yeucahat.com/search.php?s=";
		String suffixURL = "&mode=title";
		
		for (int i = 0; i < LENGTH; i++) {
			if(arrayListSong.get(i).getLyric().length() < 100){
				s = "";
				// Read html page
				BufferedReader in = new BufferedReader(
						new InputStreamReader(new URL(prefixURL
								+ arrayListSong.get(i).getName()+ suffixURL).openStream(),
								"UTF8"));
				// Set html page source to String s
				while ((inputLine = in.readLine()) != null) {
					s += inputLine;
				}
	
				org.jsoup.nodes.Document doc = Jsoup.parse(s);
	
				// Get all link search song and save to songURL array
				Elements options4 = doc.select("a.topictitle");
				int z = options4.size();
				
				if(z < 5) {
					songURL = new String[z];
					for (int j = 0; j < z; j++) {
						org.jsoup.nodes.Element options3 = doc.select("a.topictitle")
								.get(j);
						String attr3 = options3.attr("href");
						songURL[j] = prefixSongURL + attr3;
					}
				} else {
					songURL = new String[5];
					for (int j = 0; j < 5; j++) {
						org.jsoup.nodes.Element options3 = doc.select("a.topictitle")
								.get(j);
						String attr3 = options3.attr("href");
						songURL[j] = prefixSongURL + attr3;
					}
				}
	
				// Save lyric to songLyric array
				for (int k = 0; k < (z>5?5:z); k++) {
					s = "";
					BufferedReader in2 = new BufferedReader(new InputStreamReader(
							new URL(songURL[k]).openStream(), "UTF8"));
					while ((inputLine = in2.readLine()) != null) {
						s += inputLine;
					}
					org.jsoup.nodes.Document doc2 = Jsoup.parse(s);
					Elements options = doc2.select("span.lyric");
					String attr = options.text();
					if (!attr.equals("")
							&& AccentRemover.toRemoveVNese(attr).startsWith(
									AccentRemover.toRemoveVNese(arrayListSong.get(i)
											.getLyric()))) {
						System.out.println(attr);
						arrayListSong.get(i).setLyric(attr);
						break;
					} else {
						arrayListSong.get(i).setLyric("");
					}
				}
			}
		}
		return arrayListSong;
	}
}
